package com.example.treehole;

import static android.view.View.GONE;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.example.treehole.base.BaseBindingActivity;
import com.example.treehole.base.BindAdapter;
import com.example.treehole.bean.Callback;
import com.example.treehole.databinding.ActivityMyCallBackBinding;

import com.example.treehole.databinding.ItemCallbackBinding;
import com.example.treehole.util.Api;
import com.example.treehole.util.HttpUtil;

import java.util.HashMap;
import java.util.List;

public class MyCallBackActivity extends BaseBindingActivity<ActivityMyCallBackBinding> {
    private BindAdapter<ItemCallbackBinding, Callback> adapter = new BindAdapter<ItemCallbackBinding, Callback>() {
        @Override
        public ItemCallbackBinding createHolder(ViewGroup parent) {
            return ItemCallbackBinding.inflate(getLayoutInflater(), parent, false);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void bind(ItemCallbackBinding item, Callback data, int position) {
            item.tvCallback.setText("回复内容：" + (data.callback.isEmpty() ? "暂无回复" : data.callback));
            item.actionParent.setVisibility(GONE);
            item.tvContent.setText("留言内容：" + data.content);
            item.tvTime.setText("留言时间：" + data.time);
            item.tvUsername.setText("留言者：" + data.user.nickname);
            loadImage(data.user.face, item.ivImage);
            item.okBtn.setVisibility(GONE);

        }
    };



    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        viewBinder.titleTv.setText("反馈");
        viewBinder.tvCallback.setVisibility( View.VISIBLE);
        viewBinder.tvCallback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyCallBackActivity.this, CallBackActivity.class);
                startActivityForResult(intent, 1);
            }
        });
        viewBinder.postRecycler.setAdapter(adapter);
        getPostData();
    }

    private void getPostData() {
        showLoading();
        HashMap<String, Object> params = new HashMap<>();
        params.put("userid", App.user.id);
        HttpUtil.getList(Api.CALLBACK_GET, params, Callback.class, new HttpUtil.OnNetListListener<Callback>() {
            @Override
            public void success(List<Callback> data, String message) {
                dismissLoading();
                adapter.getData().clear();
                adapter.getData().addAll(data);
                adapter.notifyDataSetChanged();
                toast(message);
            }

            @Override
            public void error(String error) {
                toast(error);
                dismissLoading();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            getPostData();
        }
    }
}